package com.wrater.zero.common.utils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wrater.zero.common.base.BaseException;
import com.wrater.zero.common.base.ResultExceptionEnum;
import com.wrater.zero.common.base.ResultVo;
import com.wrater.zero.common.base.vo.BaseForm;

import java.util.List;

public class PageUtil<T extends BaseForm> {

    private PageUtil() {
        throw new RuntimeException("new PageUtil instance error");
    }


    public static void setPage(Integer pageIndex, Integer pageSize) {
        if (pageIndex == null || pageSize == null) {
            PageHelper.startPage(1, Integer.MAX_VALUE);
        } else {
            if (pageIndex <= 0 || pageSize <= 0) {
                BaseException.throwException(ResultExceptionEnum.PAGE_PARAM_ERROR, PageUtil.class);
            }
            PageHelper.startPage(pageIndex, pageSize);
        }
    }

    public static void setPage(BaseForm form) {
        setPage(form.getPageIndex(), form.getPageSize());
    }


    public static <T> PageInfo<T> getPageInfo(List<T> list) {
        return new PageInfo<T>(list);
    }


    public static <T> ResultVo<PageInfo<T>> getPageResultVo(List<T> list) {
        return new ResultVo<PageInfo<T>>(new PageInfo<T>(list));
    }
}
